<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Concept Notebook</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        /* 样式定义 */
        .tag-button {
            margin: 2px;
            padding: 5px 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #f8f9fa;
            cursor: pointer;
        }
        .tag-button:hover {
            background-color: #e9ecef;
        }
        .note-list {
            max-height: 80vh;
            overflow-y: auto;
        }
        .note-content {
            max-height: 80vh;
            overflow-y: auto;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- 第一行搜索框 -->
            <div class="col-12">
                <input type="text" id="searchInput" class="form-control" placeholder="Search...">
            </div>
        </div>
        <div class="row">
            <!-- 左侧标签和分类 -->
            <div class="col-2">
                <div id="tagContainer">
                    {% for tag in tags %}
                        <div class="tag-button" onclick="filterByTag('{{ tag }}')">{{ tag }}</div>
                    {% endfor %}
                </div>
            </div>
            <!-- 笔记列表 -->
            <div class="col-3 note-list">
                <button class="btn btn-primary mb-3" onclick="showNewNoteDialog()">Create Note</button>
                <div id="noteList">
                    {% for note in notes %}
                        <div class="note-item" onclick="openNote('{{ note.id }}')">
                            <h5>{{ note.title }}</h5>
                            <p>{{ note.content[:100] }}...</p>
                        </div>
                    {% endfor %}
                </div>
            </div>
            <!-- 笔记内容 -->
            <div class="col-7 note-content">
                <div id="noteContent"></div>
            </div>
        </div>
    </div>

    <!-- 新笔记对话框 -->
    <div class="modal" tabindex="-1" role="dialog" id="newNoteModal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <form id="newNoteForm" action="/create" method="post">
                    <div class="modal-header">
                        <h5 class="modal-title">Create New Note</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="noteTitle">Title</label>
                            <input type="text" class="form-control" id="noteTitle" name="title" required>
                        </div>
                        <div class="form-group">
                            <label for="noteContent">Content</label>
                            <textarea class="form-control" id="noteContent" name="content" rows="5" required></textarea>
                        </div>
                        <div class="form-group">
                            <label for="noteTags">Tags (comma-separated)</label>
                            <input type="text" class="form-control" id="noteTags" name="tags">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Save</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <script>
        function showNewNoteDialog() {
            $('#newNoteModal').modal('show');
        }

        function filterByTag(tag) {
            var url = new URL(window.location.href);
            var tags = url.searchParams.getAll('tags');
            if (tags.includes(tag)) {
                tags = tags.filter(t => t !== tag);
            } else {
                tags.push(tag);
            }
            url.searchParams.delete('tags');
            tags.forEach(t => url.searchParams.append('tags', t));
            window.location.href = url.toString();
        }

        function openNote(noteId) {
            $.get(`/edit/${noteId}`, function(data) {
                $('#noteContent').html(data);
            });
        }

        $(document).ready(function() {
            $('#searchInput').on('input', function() {
                var query = $(this).val().toLowerCase();
                $('.note-item').each(function() {
                    var title = $(this).find('h5').text().toLowerCase();
                    $(this).toggle(title.indexOf(query) > -1);
                });
            });
        });
    </script>
</body>
</html>
